package com.cqjtu.eecs.utils.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.entity.CourseBackGraduateRequirementTemplate;
import com.cqjtu.eecs.entity.GraduateRequirementTemplate;
import com.cqjtu.eecs.entity.dto.CourseBackGraduateRequirementTemplateDTO;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.service.ICourseBackGraduateRequirementTemplateService;
import com.cqjtu.eecs.service.ICourseService;
import com.cqjtu.eecs.service.IGraduateRequirementTemplateService;
import com.cqjtu.eecs.service.impl.CourseBackGraduateRequirementTemplateServiceImpl;
import com.cqjtu.eecs.service.impl.CourseServiceImpl;
import com.cqjtu.eecs.service.impl.GraduateRequirementTemplateServiceImpl;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class CourseBackGraduateRequirementTemplateListener extends AnalysisEventListener<CourseBackGraduateRequirementTemplateDTO> {
    // 版本编号
    private String versionCode;
    // 专业代码
    private String majorCode;

    private ICourseBackGraduateRequirementTemplateService courseBackGraduateRequirementTemplateService;
    private ICourseService courseService;
    private IGraduateRequirementTemplateService graduateRequirementTemplateService;

    private List<CourseBackGraduateRequirementTemplateDTO> list = new ArrayList<>();

    public CourseBackGraduateRequirementTemplateListener(String versionCode, String majorCode,
                                                  ICourseBackGraduateRequirementTemplateService courseBackGraduateRequirementTemplateService,
                                                  ICourseService courseService,
                                                  IGraduateRequirementTemplateService graduateRequirementTemplateService) {
        this.courseBackGraduateRequirementTemplateService = courseBackGraduateRequirementTemplateService;
        this.courseService = courseService;
        this.graduateRequirementTemplateService = graduateRequirementTemplateService;
        this.versionCode = versionCode;
        this.majorCode = majorCode;
    }

    @Override
    public void invoke(CourseBackGraduateRequirementTemplateDTO data, AnalysisContext analysisContext) {
        if (data.getCourseName() != null) {
            String courseName = data.getCourseName();
            data.setCourseName(courseName.replaceAll(" ", ""));
            list.add(data);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //sheet表中的所有课程名称
        List<String> names = this.list.stream().map(CourseBackGraduateRequirementTemplateDTO::getCourseName).collect(Collectors.toList());

        // 获取sheet表中所有课程在数据库中的课程名称代码键值对
        Map<String, String> coursesMap = courseService.listCourseNameCodeMapByNameVersionCodeAndMajorCode(names, majorCode, versionCode);

        List<GraduateRequirementTemplate> graduateRequirementTemplateList = graduateRequirementTemplateService
                .list(new LambdaQueryWrapper<GraduateRequirementTemplate>()
                        .eq(GraduateRequirementTemplate::getVersionCode, versionCode)
                        .eq(GraduateRequirementTemplate::getMajorCode, majorCode));
        Map<String, Integer> graduateRequirementTemplateMap = graduateRequirementTemplateList
                .stream()
                .collect(Collectors
                        .toMap(GraduateRequirementTemplate::getGraduateRequirementCode, GraduateRequirementTemplate::getGraduateRequirementId));
        List<CourseBackGraduateRequirementTemplate> courseBackGraduateRequirementTemplates = new ArrayList<>();
        list.forEach(item->{
            if (coursesMap.get(item.getCourseName()) == null)
                throw new NotFoundException("课程" + item.getCourseName() + "不存在，请核对信息后重新上传");
            courseBackGraduateRequirementTemplates
                    .add(new CourseBackGraduateRequirementTemplate(
                            graduateRequirementTemplateMap.get(item.getGraduateRequirementCode()),
                            coursesMap.get(item.getCourseName()),
                            majorCode,
                            versionCode,
                            Float.parseFloat(item.getCourseGraduateRequirementBackWeight())));
        });
        courseBackGraduateRequirementTemplateService.saveBatch(courseBackGraduateRequirementTemplates);
    }
}
